import { setToken, removeToken } from "@/util/auth";
import { setStore, getStore } from "@/util/store";
import { encryption, deepClone } from "@/util/util";
import {
  login,
  getUserInfo,
  getMenu,
  getTopMenu,
  menuById,
  logout,
  refeshToken,
} from "@/api/core/auth";
import { formatPath } from "@/router/avue-router";

const auth = {
  state: {
    userInfo: {},
    permission: {},
    roles: [],
    menuId: {},
    menuTop: getStore({ name: "menuTop" }) || [],
    menu: getStore({ name: "menu" }) || [],
    menuAll: getStore({ name: "menuAll" }) || [],
    token: getStore({ name: "token" }) || "",
    refreshToken: getStore({ name: "refreshToken" }) || "",
  },
  actions: {
    Login({ commit }, param = {}) {
      return new Promise((resolve, reject) => {
        login(param)
          .then((res) => {
            if (res.code && res.code === 400) {
              reject(res.message);
            }
            commit("SET_TOKEN", res.access_token);
            commit("SET_REFRESH_TOKEN", res.refresh_token);
            commit("DEL_ALL_TAG");
            resolve();
          })
          .catch((err) => {
            reject(err);
          });
      });
    },
    GetUserInfo({ commit }) {
      return new Promise((resolve, reject) => {
        getUserInfo()
          .then((res) => {
            const data = res.result;
            commit("SET_USERIFNO", data);
            commit("SET_ROLES", data.roles);
            // commit('SET_PERMISSION', data.permission)
            resolve(data);
          })
          .catch((err) => {
            reject(err);
          });
      });
    },
    // 刷新token
    RefeshToken({ state, commit }) {
      return new Promise((resolve, reject) => {
        refeshToken(state.refeshToken)
          .then((res) => {
            const data = res.data.data;
            commit("SET_TOKEN", data);
            resolve(data);
          })
          .catch((error) => {
            reject(error);
          });
      });
    },
    // 登出
    LogOut({ commit }) {
      return new Promise((resolve, reject) => {
        logout()
          .then(() => {
            commit("SET_TOKEN", "");
            commit("SET_MENUALL_NULL", []);
            commit("SET_MENU", []);
            commit("SET_ROLES", []);
            commit("DEL_ALL_TAG", []);
            removeToken();
            resolve();
          })
          .catch((error) => {
            reject(error);
          });
      });
    },
    // 注销session
    FedLogOut({ commit }) {
      return new Promise((resolve) => {
        commit("SET_TOKEN", "");
        commit("SET_MENUALL_NULL", []);
        commit("SET_MENU", []);
        commit("SET_ROLES", []);
        commit("DEL_ALL_TAG", []);
        removeToken();
        resolve();
      });
    },
    GetTopMenu({ commit }) {
      return new Promise((resolve) => {
        getTopMenu().then((res) => {
          const data = res.result || [];
          resolve(data);
        });
      });
    },
    GetMenuById({ commit }, param = {}) {
      return new Promise((resolve) => {
        menuById(param).then((res) => {
          const data = res.result;
          const menu = deepClone(data);
          menu.forEach((ele) => formatPath(ele, true));
          commit("SET_MENUALL", menu);
          commit("SET_MENU", menu);
          resolve(menu);
        });
      });
    },
    // 获取系统菜单
    GetMenu({ commit }) {
      return new Promise((resolve) => {
        getMenu().then((res) => {
          const data = res.result;
          const menu = deepClone(data);
          menu.forEach((ele) => formatPath(ele, true));
          commit("SET_MENUALL", menu);
          commit("SET_MENU", menu);
          resolve(menu);
        });
      });
    },
  },
  mutations: {
    SET_TOKEN: (state, token) => {
      setToken(token);
      state.token = token;
      setStore({ name: "token", content: state.token });
    },
    SET_REFRESH_TOKEN: (state, token) => {
      state.refresh_token = token;
      setStore({ name: "refreshToken", content: state.token });
    },
    SET_MENUID(state, menuId) {
      state.menuId = menuId;
    },
    SET_USERIFNO: (state, userInfo) => {
      state.userInfo = userInfo;
      setStore({ name: "userInfo", content: state.userInfo });
    },
    SET_MENUALL: (state, menuAll) => {
      // const menu = state.menuAll
      // menuAll.forEach(ele => {
      //   const index = menu.findIndex(item => item.path === ele.path)
      //   if (index === -1) {
      //     menu.push(ele)
      //   } else {
      //     menu[index] = ele
      //   }
      // })
      // state.menuAll = menu
      state.menuAll = menuAll;
      setStore({ name: "menuAll", content: state.menuAll });
    },
    SET_MENUALL_NULL: (state) => {
      state.menuAll = [];
      setStore({ name: "menuAll", content: state.menuAll });
    },
    SET_MENU: (state, menu) => {
      state.menu = menu;
      setStore({ name: "menu", content: state.menu });
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles;
    },
    SET_PERMISSION: (state, permission) => {
      state.permission = {};
      permission.forEach((ele) => {
        state.permission[ele] = true;
      });
    },
  },
};
export default auth;
